<?php


namespace app\management\controller;

use app\management\model\User;
use app\Request;
use liliuwei\think\Jump;
use think\facade\View;

/**
 * Class AuthorizedDomain
 * @package app\management\controller
 *
 * 域名授权
 */
class AuthorizedDomain
{
    use Jump;
    public function index(){
        return "this is a AuthorizedDomain";
    }


    public function saveDomain(Request $request){
//        return "this is a saveDomain function";

        if ($request->isPost()){

            $userData = get_admin_session();
            $user = new User();
            $userOne = $user->where('id',$userData['id'])->find();
            $oldDomain = $userOne->user_domain;
            //检查两个数组不是否存在相同项
//            print_r(array_diff($oldDomain,explode('|',$request->param('domain')))) ;
            $flag = strstr($request->param('domain'),'|');
            //判断是输入一个还是多个
            if ($flag){
                //输入多个
                if (is_null($oldDomain)){
                    //判断是否域名列表为空
                    $oldDomain=[];
                }
                $newDomain = array_diff(explode('|',$request->param('domain')),$oldDomain);
                array_push($oldDomain,$newDomain);
            }else{
                if (is_null($oldDomain)){
                    $oldDomain=[];
//                    array_push($oldDomain,$newDomain);
                }
                    $newDomain = in_array($request->param('domain'),$oldDomain);
                    if (!$newDomain){
                        array_push($oldDomain,$request->param('domain'));
                    }else{
                        return $this->error('该域名已经存在');
                    }

            }



            $userOne->user_domain = $oldDomain;
//            $arr = $userOne->user_domain;
//            var_dump($userOne->user_domain);
//            array_push($userOne->user_domain);
            $userOne->save();
            return $request->param('domain');


        }
        return View::fetch();

    }


    /**
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 添加授权域名
     */
    public function addDomain(Request $request){
        $arr = textareaToStr($request->param('domain'));
        if (empty($arr)) {
            return $this->success('请输入需要绑定的域名~~');
        }
        $user = new User();
        $userDate = get_admin_session();
        $userDomain = $user->where('id',$userDate['id'])->find();
//        var_dump($userDomain->user_domain);
        if (is_null($userDomain->user_domain)){
            $userDomain->user_domain=array();
        }
        $all =array_unique(array_merge($userDomain->user_domain,$arr));
        $userDomain->user_domain = $all;
        $userDomain->save();
        return $this->success('授权成功~~');
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 显示授权域名
     */
    public function domainList(Request $request){
        $user = new User();
        $userDate = get_admin_session();
        $userDomain = $user->where('id',$userDate['id'])->find();
        $domian  = $userDomain->user_domain;

        if (is_null($domian)){
            return lay_msg(1,'暂无数据',0);
        }
        $msgList = array();

        foreach ($domian as $k => $v){
            $msgList[$k]['id']= $k;
            $msgList[$k]['domain']= $v;
        }
     
    //    var_dump($msgList);
        return lay_msg(0,'success',count($msgList),$msgList);
    }

    // 搜索授权域名
    public function seacherDomainList(Request $request){
        $seacherDomain = textareaToStr($request->param('domain'));
        $user = new User();
        $userDate = get_admin_session();
        $userDomain = $user->where('id',$userDate['id'])->find();
        $domain  = $userDomain->user_domain;

        if (is_null($domain) && !in_array($seacherDomain,$domain)){
            return lay_msg(1,'暂无数据',0);
        }
        $msgList = array();
        foreach ($domain as $k => $v){
                $msgList[$k]['id']= $k;
                $msgList[$k]['domain']= $v;
        }
        $msgLists = array();
        for($i=0;$i<count($msgList);$i++){
            if (array($msgList[$i]['domain']) == $seacherDomain) {
                $msgLists[]=$msgList[$i];
            }
            // $msgLists=$msgList[$i]['domian'];
        }
        return lay_msg(0,'success',count($msgLists),$msgLists);
     
        
    }



    public function deleteDomain(Request $request){
        $user = new User();
        $userDate = get_admin_session();
        $userDomain = $user->where('id',$userDate['id'])->find();
        $domian  = $userDomain->user_domain;
        unset($domian[$request->param('id')]);
        // var_dump($domian);
        // exit;
        // 使用array_values解决保存绑定域名不知为何改为花括号存储的问题
        $userDomain->user_domain = array_values($domian);
        $userDomain->save();

        return $this->success('删除成功~~');
    }

}
